package com.sjqi.demo.sqlsession;

import com.sjqi.demo.config.Configuration;
import com.sjqi.demo.executor.Executor;
import com.sjqi.demo.factory.SqlSessionFactory;

import java.util.List;
import java.util.Map;

/**
 * @author qishaojun
 */
public class DefaultSqlSession implements SqlSession {
    Configuration configuration;

    //TODO:Configuration 注入
    @Override
    public <T> List<T> selectList(String id, Map<String, Object> map) {
        //Executor 有多种类型
        // configuration 里面的settings 可以指定Executor类型 或者用默认的
        //可以在创建SqlSession时指定创建Executor 的类型
        Executor executor = configuration.newExecutor(null);
        MappedStatement mappedStatement = configuration.getMappedStatement();

        return executor.query(configuration, mappedStatement);
    }

    @Override
    public <T> T selectOne(String statementId, Map<String, Object> map) {
        List<T> list = this.selectList(statementId, map);
        if (null != list && list.size() == 1) {
            return list.get(1);
        }
        return null;
    }
}
